package com.ryder.petmatediarybackend.module.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.admin.dto.AuditDTO;
import com.ryder.petmatediarybackend.module.admin.service.AuditManagementService;
import com.ryder.petmatediarybackend.module.admin.vo.AuditItemVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：审核管理 Controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025/10/31
 */
@Slf4j
@Tag(name = "审核管理", description = "店铺资质、店铺申请、内容审核等管理接口（需要登录 + 管理员角色 + 对应权限）")
@RestController
@RequestMapping("/admin/audit")
@SaCheckRole(value = {"ROLE_REGION_AUDITOR", "ROLE_REGION_MANAGER", "ROLE_PLATFORM_ADMIN"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class AdminAuditController {

    private final AuditManagementService auditManagementService;

    // ==================== 获取待审核列表 ====================

    @Operation(summary = "获取所有待审核项目", description = "获取所有类型的待审核项目综合列表")
    @SaCheckPermission("audit:list")
    @GetMapping("/pending/all")
    public Result<List<AuditItemVO>> getAllPendingAudits() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 获取所有待审核项目", userId);
        List<AuditItemVO> list = auditManagementService.getAllPendingAudits();
        log.info("[审核管理] 用户ID={} 获取待审核项目成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "获取待审核店铺资质列表", description = "获取所有待审核的店铺资质申请")
    @SaCheckPermission("audit:qualification:list")
    @GetMapping("/pending/qualifications")
    public Result<List<AuditItemVO>> getPendingQualifications() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 获取待审核资质列表", userId);
        List<AuditItemVO> list = auditManagementService.getPendingQualifications();
        log.info("[审核管理] 用户ID={} 获取待审核资质成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "获取待审核店铺申请列表", description = "获取所有待审核的店铺申请")
    @SaCheckPermission("audit:shop:list")
    @GetMapping("/pending/shops")
    public Result<List<AuditItemVO>> getPendingShops() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 获取待审核店铺列表", userId);
        List<AuditItemVO> list = auditManagementService.getPendingShops();
        log.info("[审核管理] 用户ID={} 获取待审核店铺成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "获取待审核宠物合集列表", description = "获取所有待审核的宠物合集")
    @SaCheckPermission("audit:pet:list")
    @GetMapping("/pending/pets")
    public Result<List<AuditItemVO>> getPendingPetCollections() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 获取待审核宠物合集列表", userId);
        List<AuditItemVO> list = auditManagementService.getPendingPetCollections();
        log.info("[审核管理] 用户ID={} 获取待审核宠物合集成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "获取待审核商品合集列表", description = "获取所有待审核的商品合集")
    @SaCheckPermission("audit:product:list")
    @GetMapping("/pending/products")
    public Result<List<AuditItemVO>> getPendingProductCollections() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 获取待审核商品合集列表", userId);
        List<AuditItemVO> list = auditManagementService.getPendingProductCollections();
        log.info("[审核管理] 用户ID={} 获取待审核商品合集成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "获取待审核说说列表", description = "获取所有待审核的说说")
    @SaCheckPermission("audit:post:list")
    @GetMapping("/pending/posts")
    public Result<List<AuditItemVO>> getPendingSocialPosts() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 获取待审核说说列表", userId);
        List<AuditItemVO> list = auditManagementService.getPendingSocialPosts();
        log.info("[审核管理] 用户ID={} 获取待审核说说成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    // ==================== 审核操作 ====================

    @Operation(summary = "审核店铺资质", description = "审核通过或驳回店铺资质申请")
    @SaCheckPermission("audit:qualification:approve")
    @PostMapping("/qualification")
    public Result<String> auditQualification(@Valid @RequestBody AuditDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 审核店铺资质, targetId={}, status={}", userId, dto.getTargetId(), dto.getAuditStatus());
        auditManagementService.auditQualification(userId, dto);
        String result = dto.getAuditStatus() == 1 ? "审核通过" : "审核驳回";
        log.info("[审核管理] 用户ID={} 审核店铺资质成功, result={}", userId, result);
        return Result.ok(result);
    }

    @Operation(summary = "审核店铺申请", description = "审核通过或驳回店铺申请")
    @SaCheckPermission("audit:shop:approve")
    @PostMapping("/shop")
    public Result<String> auditShop(@Valid @RequestBody AuditDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 审核店铺申请, targetId={}, status={}", userId, dto.getTargetId(), dto.getAuditStatus());
        auditManagementService.auditShop(userId, dto);
        String result = dto.getAuditStatus() == 1 ? "审核通过" : "审核驳回";
        log.info("[审核管理] 用户ID={} 审核店铺申请成功, result={}", userId, result);
        return Result.ok(result);
    }

    @Operation(summary = "审核宠物合集", description = "审核通过或驳回宠物合集")
    @SaCheckPermission("audit:pet:approve")
    @PostMapping("/pet")
    public Result<String> auditPetCollection(@Valid @RequestBody AuditDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 审核宠物合集, targetId={}, status={}", userId, dto.getTargetId(), dto.getAuditStatus());
        auditManagementService.auditPetCollection(userId, dto);
        String result = dto.getAuditStatus() == 1 ? "审核通过" : "审核驳回";
        log.info("[审核管理] 用户ID={} 审核宠物合集成功, result={}", userId, result);
        return Result.ok(result);
    }

    @Operation(summary = "审核商品合集", description = "审核通过或驳回商品合集")
    @SaCheckPermission("audit:product:approve")
    @PostMapping("/product")
    public Result<String> auditProductCollection(@Valid @RequestBody AuditDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 审核商品合集, targetId={}, status={}", userId, dto.getTargetId(), dto.getAuditStatus());
        auditManagementService.auditProductCollection(userId, dto);
        String result = dto.getAuditStatus() == 1 ? "审核通过" : "审核驳回";
        log.info("[审核管理] 用户ID={} 审核商品合集成功, result={}", userId, result);
        return Result.ok(result);
    }

    @Operation(summary = "审核说说", description = "审核通过或驳回说说")
    @SaCheckPermission("audit:post:approve")
    @PostMapping("/post")
    public Result<String> auditSocialPost(@Valid @RequestBody AuditDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[审核管理] 用户ID={} 审核说说, targetId={}, status={}", userId, dto.getTargetId(), dto.getAuditStatus());
        auditManagementService.auditSocialPost(userId, dto);
        String result = dto.getAuditStatus() == 1 ? "审核通过" : "审核驳回";
        log.info("[审核管理] 用户ID={} 审核说说成功, result={}", userId, result);
        return Result.ok(result);
    }
}

